using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Params;
using ApiTest.Api.Repository;
using ApiTest.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;

namespace ApiTest.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
         private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private TokenParameter _tokenParameter;

        public UsersController(IConfiguration configuration, IRepository<Users> usersRepository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }
        public dynamic Get()
        {
            var users = _usersRepository.Table.ToList();
            return new
            {
                Code = 200,
                Data = users,
                Msg = "请求成功"
            };
        }


        [HttpGet("{id}")]
        private dynamic GetUsers(int id)
        {
            var user = _usersRepository.GetById(id);
            return new
            {
                Code = 200,
                Data = user,
                Msg = "成功获取指定用户"
            };
        }
         [HttpPost]
        public dynamic Post(CreateUser newUser)
        {
            var username = newUser.Username.Trim();
            var password = newUser.Password.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "用户名或密码不能为空"
                };
            }


            var user = new Users
            {
                Username = newUser.Username,
                Password = newUser.Password
            };

            _usersRepository.Insert(user);
            return new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功^_^"
            };
        }

        [HttpPut("{id}")]

        public dynamic Put(int id,CreateUser updateUsers)
        {

            var username = updateUsers.Username.Trim();
            var password = updateUsers.Password.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "用户名或密码不能为空"
                };
            }


             var users = _usersRepository.GetById(id);

             if(users == null)
             {
                 return new
                 {
                     Code = 1000,
                     Data = "",
                     Msg = "找不到要更新的用户"
                 };
             }

            // var users = new Users
            // {
            //     Username = updateUsers.Username,
            //     Password = updateUsers.Password
            // };

            users.Username = updateUsers.Username.Trim();
            users.Password = updateUsers.Password.Trim();

            _usersRepository.Update(users);

            return new
            {
                Code = 200,
                Data = users,
                Msg = "修改成功"
            };
        }

        [HttpDelete("{id}")]

        public dynamic Delete(int id)
        {
            _usersRepository.Delete(id);
            return new 
            {
                Code = 200,
                Data = "",
                Msg = "删除成功"
            };
        }
          [AllowAnonymous]
       [HttpPost,Route("token")]
        public dynamic GetToken(CreateUser newUser)
        {
            var username = newUser.Username.Trim();
            var password = newUser.Password.Trim();

            var user=_usersRepository.Table.Where(x =>x.Username==username && x.Password==password).FirstOrDefault();

            if(user==null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "用户名或密码不正确，请确认后重试"
                };
            }

            var token=TokenHelper.GenerateToekn(_tokenParameter,user.ToString());
            var refreshToken="112358";
            
            return new
            {
                Code = 1000,
                Data = new {Token =token,refreshToken=refreshToken},
                Msg = "用户登录成功^_^"
            };
        }

          [AllowAnonymous]
        [HttpPost, Route("refreshtoken")]
        public dynamic RefreshToken(RefreshTokenDTO refresh)
        {
            var username = TokenHelper.ValidateToken(_tokenParameter, refresh);

            if (string.IsNullOrEmpty(username))
            {
                return new { Code = 1002, Data = "", Msg = "token验证失败" };
            }

            var token = TokenHelper.GenerateToekn(_tokenParameter,username);
            var refreshToken = "112358";

            return new {
                Code = 1000,
                Data = new { Token = token, refreshToken = refreshToken },
                Msg = "刷新token成功^_^"
            };
        }
        
    }
}